/**
 * 简历宽列表
 */
import React from 'react';
import { Table, Row } from 'antd';
import { TableFilter } from '@/components';
import { getDisplayText } from '@/utils/dataDictUtil';
import { eventTrack } from '@/utils/track';

import ThumbCommentGroup from './ThumbCommentGroup';
import { Operates } from '../components';
import {
    RecommendOperate,
    TransferOperate,
    TalentMajorInfoCard,
    TalentsBlackListOperate
} from './index';
import { renderShortDate, renderResumeStatus } from '../renderUtil';
import { RESUME_STATUS } from '../dictionary';

const ResumeCardTable = ({
    loading,
    dataSource,
    condition,
    onJumpDetail,
    onRowChange,
    onRefresh,
    isPrincipal,
    isLauncher,
    channelDict,
    recruitDetail,
    ...rest
}) => {
    const handleChange = (id, res) => {
        if (onRowChange) {
            onRowChange(id, res);
        }
    };

    const columns = [
        {
            title: <span style={{ marginLeft: '16px' }}>姓名</span>,
            dataIndex: 'talentName',
            render: (_, record) => (
                <div style={{ cursor: 'pointer' }}>
                    <TalentMajorInfoCard talent={record} />
                </div>
            )
        },
        {
            title: '渠道',
            dataIndex: 'notesSourceList',
            filteredValue: condition.notesSourceList || [],
            filters: channelDict,
            filterMultiple: true,
            render: (val, record) => getDisplayText(channelDict, record.notesSource)
        },
        {
            title: '申请时间',
            dataIndex: 'applyTime',
            render: val => renderShortDate(val)
        },
        {
            title: '状态',
            dataIndex: 'status',
            filteredValue: condition.status ? [condition.status] : [],
            filters: RESUME_STATUS,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: renderResumeStatus
        },
        {
            title: '操作',
            dataIndex: 'action',
            width: 230,
            onCell: () => ({
                onClick: e => {
                    e.stopPropagation();
                }
            }),
            render: (_, record) => {
                const {
                    praiseEmployeeNameList = [],
                    trampleEmployeeNameList = [],
                    evaluateType,
                    id,
                    talentCode,
                    hasBlackList,
                    resumeStep,
                    status,
                    recruitList = []
                } = record;
                const defaultNoticeEmployee = new Set();
                recruitList.forEach(item => {
                    defaultNoticeEmployee.add(item.launchEmployeeCode);
                    defaultNoticeEmployee.add(item.principalEmployeeCode);
                });
                const operates = !hasBlackList ? [
                    <ThumbCommentGroup
                        onChange={handleChange}
                        initState={{ praiseEmployeeNameList, trampleEmployeeNameList, evaluateType }}
                        resumeId={id}
                        record={record}
                        onRefresh={onRefresh}
                    ></ThumbCommentGroup>,
                    <Row
                        type="flex"
                        align="middle"
                        onClick={() => {
                            eventTrack({
                                eventName: 'personnel_management_recruitment_demand_list_detail_resume_operator_click',
                                pageName: 'personnel_management_recruitment',
                                eventProps: {
                                    click_context: `${recruitDetail.dutyName}-${record.talentName}-推荐简历`
                                }
                            });
                        }}
                    >
                        <RecommendOperate
                            resumeId={id}
                            onConfirm={onRefresh}
                            weedOutDisabled={!(isPrincipal || isLauncher)}
                        />
                    </Row>,
                    <Row
                        type="flex"
                        align="middle"
                        onClick={() => {
                            eventTrack({
                                eventName: 'personnel_management_recruitment_demand_list_detail_resume_operator_click',
                                pageName: 'personnel_management_recruitment',
                                eventProps: {
                                    click_context: `${recruitDetail.dutyName}-${record.talentName}-转发简历`
                                }
                            });
                        }}
                    >
                        <TransferOperate resumeId={id} />
                    </Row>,
                    <TalentsBlackListOperate
                        operateType="add"
                        talentCode={talentCode}
                        onRefresh={onRefresh}
                        defaultNoticeEmployee={Array.from(defaultNoticeEmployee)}
                        messageType={[3].includes(status) ? 0 : 2} // 同人才库加入黑名单判断条件，简历阶段非淘汰type为2
                        resumeStep={resumeStep}
                    />
                ] : [
                    <TalentsBlackListOperate
                        operateType="remove"
                        talentCode={talentCode}
                        onRefresh={onRefresh}
                        defaultNoticeEmployee={Array.from(defaultNoticeEmployee)}
                        messageType={[3].includes(status) ? 0 : 2} // 同人才库加入黑名单判断条件，简历阶段非淘汰type为2
                        resumeStep={resumeStep}
                    />
                ];
                return (
                    // onChange 返回 新的{ praiseEmployeeNameList , trampleEmployeeNameList, evaluateType, status }
                    <Operates operates={operates} />
                );
            }
        }
    ];

    React.useEffect(() => {
        const sourceListText = condition?.notesSourceList.map(item => getDisplayText(channelDict, item)).join(',');
        eventTrack({
            eventName: 'personnel_management_recruitment_demand_list_detail_resume_column_screen_click',
            pageName: 'personnel_management_recruitment',
            eventProps: {
                click_context: `${recruitDetail.dutyName}-渠道筛选-${sourceListText}`
            }
        });
    }, [JSON.stringify(condition.notesSourceList)]);

    return (
        <Table
            {...rest}
            rowKey="id"
            bordered={false}
            columns={columns}
            loading={loading}
            dataSource={dataSource}
            pagination={false}
            onRow={record => ({
                onClick: () => {
                    const { id } = record;
                    if (onJumpDetail) onJumpDetail(id, record);
                }
            })}
        />
    );
};
export default ResumeCardTable;
